/**
 * @Copyright ®2015 Sinosoft Co. Ltd. All rights reserved.
 * 项目名称 : 网站服务平台
 * 创建日期 : 2016-11-14
 * 修改历史 :
 * 1. [2016-11-14]创建文件 by chenxin
 */
package com.sinosoft.sinopay.web.gateway.biz.service.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sinosoft.sinocloud.common.biz.service.ReportLogService;
import com.sinosoft.sinocloud.platform.base.SinoBaseService;
import com.sinosoft.sinocloud.platform.common.exception.biz.IllegalRequestException;
import com.sinosoft.sinocloud.platform.common.exception.checked.RepeatRequestException;
import com.sinosoft.sinocloud.platform.common.util.MathUtil;
import com.sinosoft.sinopay.merchants.facade.MerchantInfoFacadeService;
import com.sinosoft.sinopay.payrules.config.ENUM_IP_BIZTYPE;
import com.sinosoft.sinopay.payrules.config.ENUM_IP_SECURITYLIST;
import com.sinosoft.sinopay.payrules.facade.SecurityListFacadeService;
import com.sinosoft.sinopay.web.gateway.biz.service.GatewaySecurityService;
import com.sinosoft.sinopay.web.gateway.dto.PayEntryRequestData;
import com.sinosoft.sinopay.web.util.GatewaySignUtil;

/**
 * 【网关支付安全服务实现类】
 * @author chenxin
 * @date 2016-11-14 上午11:51 
 * @version V1.0
 */
@Service
public class GatewaySecurityServiceImpl extends SinoBaseService implements GatewaySecurityService{
    @Autowired
    private MerchantInfoFacadeService merchantInfoFacadeService;
    @Autowired
    private SecurityListFacadeService securityListFacadeService;
    @Autowired
    private ReportLogService reportLogService;
    /** 商户签名信息缓存map */
    private  Map<String,String> signKeysMap;
    /** IP白名单缓存 */
    private List<String> ipWhiteList;
    /** IP黑名单缓存 */
    private List<String> ipBlackList;

    @Override
    public void checkMerchantSignValid(PayEntryRequestData payEntryRequestData, String signMsg) throws IllegalRequestException {
        if(signKeysMap ==null || signKeysMap.size() == 0)
            signKeysMap = merchantInfoFacadeService.getAllMerchantSignKeys();

        String tSignKey = signKeysMap.get(payEntryRequestData.getMerchantCode());

        Map<String,String> tSignInfoMap = new HashMap<String,String>();
        tSignInfoMap.put("serialNo",payEntryRequestData.getSerialNo());//交易流水号
        tSignInfoMap.put("bizNo",payEntryRequestData.getBizNo());//业务号码
        tSignInfoMap.put("merchantCode",payEntryRequestData.getMerchantCode());//商户编码
        tSignInfoMap.put("transDesc",payEntryRequestData.getTransDesc());//交易描述
        tSignInfoMap.put("bizType",payEntryRequestData.getBizType());
        tSignInfoMap.put("accountName",payEntryRequestData.getAccountName());
        tSignInfoMap.put("idType",payEntryRequestData.getIdType());
        tSignInfoMap.put("idNo",payEntryRequestData.getIdNo());
        tSignInfoMap.put("bankName",payEntryRequestData.getBankName());
        tSignInfoMap.put("bankCode",payEntryRequestData.getBankCode());
        tSignInfoMap.put("accountNo",payEntryRequestData.getAccountNo());
        tSignInfoMap.put("mobile",payEntryRequestData.getMobile());
        if(payEntryRequestData.getPayAmount()==null || payEntryRequestData.getPayAmount().equals(0)){
       	 	throw new IllegalRequestException("金额不能为空");
        }
        String tPayAmount = transAmount(payEntryRequestData.getPayAmount());
        tSignInfoMap.put("payAmount", tPayAmount);//支付金额
        String tCompareSignMsg = GatewaySignUtil.encryMD5(tSignInfoMap,tSignKey);
        if(signMsg ==null || !signMsg.equals(tCompareSignMsg)){
            throw new IllegalRequestException("签名不合法");
        }
    }

    private String transAmount(BigDecimal tPayAmount) {
        BigDecimal fen = tPayAmount.multiply(new BigDecimal("100"));
        long fenAmount = fen.intValue();
        String money = String.valueOf(fenAmount);
        return money;
	}

	@Override
    public void checkRepeatRequest(String merchantCode, String serialNo) throws RepeatRequestException {
        reportLogService.checkRepeatRequest(merchantCode,serialNo);
    }

    @Override
    public void checkRequestIpValid(String tRequestIp) throws IllegalRequestException {
        securityListFacadeService.checkIpValid(tRequestIp, ENUM_IP_BIZTYPE.GATEWAY, ENUM_IP_SECURITYLIST.BLACK);
    }

    @Override
    public String createSignMsg(String tMerchantCode, Map<String,String> toSignMsg) {
        if(signKeysMap ==null || signKeysMap.size() == 0)
            signKeysMap = merchantInfoFacadeService.getAllMerchantSignKeys();
        String tSignKey = signKeysMap.get(tMerchantCode);
        return GatewaySignUtil.encryMD5(toSignMsg,tSignKey);
    }
}
